﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;
namespace BaHuan
{
    public partial class FrmDinhKhoanPhieuHangTra : Form
    {
        private ChiNhanhCongTyMgt chinhanhcongtyMgt = new ChiNhanhCongTyMgt();
        private TaiKhoanKeToanMgt taikhoanketoanMgt = new TaiKhoanKeToanMgt();
        private ButToanMgt buttoanMgt = new ButToanMgt();

        private PhieuHangTra phieuhangtra;
        public PhieuHangTra PhieuHangTra
        {
            get { return this.phieuhangtra; }
            set { this.phieuhangtra = value; }
        }

        public FrmDinhKhoanPhieuHangTra()
        {
            InitializeComponent();
        }

        private void FrmDinhKhoanPhieuHangTra_Load(object sender, EventArgs e)
        {
            dgvDinhKhoan1.Columns["colSoTien1"].ReadOnly = false;
            dgvDinhKhoan2.Columns["colSoTien2"].ReadOnly = false;
            txtSoChungTu.Text = this.phieuhangtra.MaSo;
            txtMSKH.Text = this.phieuhangtra.KhachHang;
            List<ChiNhanhCongTy> lstChiNhanhCty = (from cn in GetAllChiNhanhCongTy()
                                                   where cn.Id == this.phieuhangtra.IdChiNhanhCongTy
                                                   select cn).ToList();
            if(lstChiNhanhCty.Count > 0)
                txtChiNhanh.Text = lstChiNhanhCty[0].TenChiNhanh;

            cboSoTK1.DataSource = GetAllTaiKhoanKeToan();
            cboSoTK1.DisplayMember = "MaSoTK";
            cboSoTK1.ValueMember = "Id";

            cboSoTK2.DataSource = GetAllTaiKhoanKeToan();
            cboSoTK2.DisplayMember = "MaSoTK";
            cboSoTK2.ValueMember = "Id";

            LoadToDataGridView();

            dtpNgay.Select();
        }

        private void dgvDinhKhoan1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex > -1)
            {
                ResultObj result = new ResultObj();
                List<TaiKhoanKeToan> lstTaiKhoanKeToan = (from tkkt in GetAllTaiKhoanKeToan()
                                                          where tkkt.Id == Convert.ToInt32(dgvDinhKhoan1.Rows[e.RowIndex].Cells[0].Value.ToString())
                                                          select tkkt).ToList();
                string tenTK = lstTaiKhoanKeToan[0].TenTK;

                dgvDinhKhoan1.Rows[e.RowIndex].Cells[2].Value = tenTK;
                dgvDinhKhoan1.Rows[e.RowIndex].Cells[3].Value = 0;
            }

            if (e.ColumnIndex == 3 && e.RowIndex > -1 && dgvDinhKhoan1.Rows[e.RowIndex].Cells[0].FormattedValue.ToString() != "632")
            {
                if (dgvDinhKhoan1.Rows[e.RowIndex].Cells[3].Value == null)
                    dgvDinhKhoan1.Rows[e.RowIndex].Cells[3].Value = 0;
            }
            long tongMotBaMot = 0;
            for (int j = 1; j < dgvDinhKhoan1.Rows.Count - 1; j++)
            {
                if (dgvDinhKhoan1.Rows[j].Cells[3].Value != null)
                {
                    tongMotBaMot += Convert.ToInt64(dgvDinhKhoan1.Rows[j].Cells[3].Value.ToString());
                }
            }
            dgvDinhKhoan1.Rows[0].Cells[3].Value = tongMotBaMot;
        }

        private void dgvDinhKhoan2_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == 0 && e.RowIndex > -1)
            {
                ResultObj result = new ResultObj();
                List<TaiKhoanKeToan> lstTaiKhoanKeToan = (from tkkt in GetAllTaiKhoanKeToan()
                                                          where tkkt.Id == Convert.ToInt32(dgvDinhKhoan2.Rows[e.RowIndex].Cells[0].Value.ToString())
                                                          select tkkt).ToList();
                string tenTK = lstTaiKhoanKeToan[0].TenTK;

                dgvDinhKhoan2.Rows[e.RowIndex].Cells[2].Value = tenTK;
                dgvDinhKhoan2.Rows[e.RowIndex].Cells[3].Value = 0;
            }

            if (e.ColumnIndex == 3 && e.RowIndex > -1 && dgvDinhKhoan2.Rows[e.RowIndex].Cells[0].FormattedValue.ToString() != "131")
            {
                if (dgvDinhKhoan2.Rows[e.RowIndex].Cells[3].Value == null)
                    dgvDinhKhoan2.Rows[e.RowIndex].Cells[3].Value = 0;
            }
            long tongMotBaMot = 0;
            for (int j = 1; j < dgvDinhKhoan2.Rows.Count - 1; j++)
            {
                if (dgvDinhKhoan2.Rows[j].Cells[3].Value != null)
                {
                    tongMotBaMot += Convert.ToInt64(dgvDinhKhoan2.Rows[j].Cells[3].Value.ToString());
                }
            }
            dgvDinhKhoan2.Rows[0].Cells[3].Value = tongMotBaMot;
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (dgvDinhKhoan1.Rows[0].Cells[1].FormattedValue.ToString() == "Nợ")
            {
                MessageBox.Show("Định khoản " + dgvDinhKhoan1.Rows[0].Cells[1].FormattedValue.ToString() + " phải chọn \"Có\" !!!", "Thông báo");
                return;
            }

            for (int i = 1; i < dgvDinhKhoan1.Rows.Count - 1; i++)
            {
                if ((dgvDinhKhoan1.Rows[i].Cells[0].FormattedValue.ToString() == "" || dgvDinhKhoan1.Rows[i].Cells[1].FormattedValue.ToString() == ""))
                {
                    MessageBox.Show("Vui lòng nhập đầy đủ thông tin trong danh sách !", "Thông báo");
                    return;
                }
            }

            for (int i = 1; i < dgvDinhKhoan1.Rows.Count - 1; i++)
            {
                if (dgvDinhKhoan1.Rows[i].Cells[1].FormattedValue.ToString() == "Có")
                {
                    MessageBox.Show("Các định khoản khác " + dgvDinhKhoan1.Rows[0].Cells[0].FormattedValue.ToString() + " phải chọn \"Nợ\" !!!", "Thông báo");
                    return;
                }
            }

            if (dgvDinhKhoan2.Rows[0].Cells[1].FormattedValue.ToString() == "Nợ")
            {
                MessageBox.Show("Định khoản " + dgvDinhKhoan2.Rows[0].Cells[1].FormattedValue.ToString() + " phải chọn \"Có\" !!!", "Thông báo");
                return;
            }

            for (int i = 1; i < dgvDinhKhoan2.Rows.Count - 1; i++)
            {
                if ((dgvDinhKhoan2.Rows[i].Cells[0].FormattedValue.ToString() == "" || dgvDinhKhoan2.Rows[i].Cells[1].FormattedValue.ToString() == ""))
                {
                    MessageBox.Show("Vui lòng nhập đầy đủ thông tin trong danh sách !", "Thông báo");
                    return;
                }
            }

            for (int i = 1; i < dgvDinhKhoan2.Rows.Count - 1; i++)
            {
                if (dgvDinhKhoan2.Rows[i].Cells[1].FormattedValue.ToString() == "Có")
                {
                    MessageBox.Show("Các định khoản khác " + dgvDinhKhoan2.Rows[0].Cells[0].FormattedValue.ToString() + " phải chọn \"Nợ\" !!!", "Thông báo");
                    return;
                }
            }

            EditOrRemoveDinhKhoan(phieuhangtra.MaSo);

            ResultObj result = new ResultObj();

            for (int i = 1; i < dgvDinhKhoan1.Rows.Count - 1; i++)
            {
                if (dgvDinhKhoan1.Rows[i].Cells[3].FormattedValue.ToString() != "0")
                {
                    ButToan buttoan = new ButToan();
                    buttoan.NgayThang = dtpNgay.Value;
                    buttoan.ChiNhanh = txtChiNhanh.Text;
                    buttoan.MaSoKhachHang = txtMSKH.Text;
                    buttoan.DienGiai = txtNoiDung.Text;
                    buttoan.SoChungTu = txtSoChungTu.Text;
                    buttoan.TKGoc = dgvDinhKhoan1.Rows[0].Cells[0].FormattedValue.ToString();
                    buttoan.TKDoiUng = dgvDinhKhoan1.Rows[i].Cells[0].FormattedValue.ToString();
                    buttoan.GhiNo = 0;
                    buttoan.GhiCo = Convert.ToInt64(dgvDinhKhoan1.Rows[i].Cells[3].Value.ToString());
                    result = buttoanMgt.SaveNewButToan(buttoan);

                    buttoan = new ButToan();
                    buttoan.NgayThang = dtpNgay.Value;
                    buttoan.ChiNhanh = txtChiNhanh.Text;
                    buttoan.MaSoKhachHang = txtMSKH.Text;
                    buttoan.DienGiai = txtNoiDung.Text;
                    buttoan.SoChungTu = txtSoChungTu.Text;
                    buttoan.TKGoc = dgvDinhKhoan1.Rows[i].Cells[0].FormattedValue.ToString();
                    buttoan.TKDoiUng = dgvDinhKhoan1.Rows[0].Cells[0].FormattedValue.ToString();
                    buttoan.GhiNo = Convert.ToInt64(dgvDinhKhoan1.Rows[i].Cells[3].FormattedValue.ToString());
                    buttoan.GhiCo = 0;
                    result = buttoanMgt.SaveNewButToan(buttoan);
                }
            }

            for (int i = 1; i < dgvDinhKhoan2.Rows.Count - 1; i++)
            {
                if (dgvDinhKhoan2.Rows[i].Cells[3].FormattedValue.ToString() != "0")
                {
                    ButToan buttoan = new ButToan();
                    buttoan.NgayThang = dtpNgay.Value;
                    buttoan.ChiNhanh = txtChiNhanh.Text;
                    buttoan.MaSoKhachHang = txtMSKH.Text;
                    buttoan.DienGiai = txtNoiDung.Text;
                    buttoan.SoChungTu = txtSoChungTu.Text;
                    buttoan.TKGoc = dgvDinhKhoan2.Rows[0].Cells[0].FormattedValue.ToString();
                    buttoan.TKDoiUng = dgvDinhKhoan2.Rows[i].Cells[0].FormattedValue.ToString();
                    buttoan.GhiNo = 0;
                    buttoan.GhiCo = Convert.ToInt64(dgvDinhKhoan2.Rows[i].Cells[3].Value.ToString());
                    result = buttoanMgt.SaveNewButToan(buttoan);

                    buttoan = new ButToan();
                    buttoan.NgayThang = dtpNgay.Value;
                    buttoan.ChiNhanh = txtChiNhanh.Text;
                    buttoan.MaSoKhachHang = txtMSKH.Text;
                    buttoan.DienGiai = txtNoiDung.Text;
                    buttoan.SoChungTu = txtSoChungTu.Text;
                    buttoan.TKGoc = dgvDinhKhoan2.Rows[i].Cells[0].FormattedValue.ToString();
                    buttoan.TKDoiUng = dgvDinhKhoan2.Rows[0].Cells[0].FormattedValue.ToString();
                    buttoan.GhiNo = Convert.ToInt64(dgvDinhKhoan2.Rows[i].Cells[3].FormattedValue.ToString());
                    buttoan.GhiCo = 0;
                    result = buttoanMgt.SaveNewButToan(buttoan);
                }
            }

            if (result.ReturnCode > 0)
            {
                MessageBox.Show("Lưu thành công", "Thông báo");
                this.Close();
            }
            else if (result.ReturnCode == Definitions.DUPLICATED_VALUE)
                MessageBox.Show("Dữ liệu bị trùng", "Thông báo");
            else
                MessageBox.Show("Thất bại");
        }

        public void EditOrRemoveDinhKhoan(string MaSo)
        {
            ResultObj resultObj = new ResultObj();

            List<ButToan> lst = (from p in GetAllButToan()
                                 where p.SoChungTu == MaSo
                                 select p).ToList();

            if (lst.Count > 0)
            {
                foreach (ButToan b in lst)
                {
                    b.IsEdited = 1;
                    resultObj = buttoanMgt.UpdateButToan(b);
                }
            }
        }

        public List<ButToan> GetAllButToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButToan();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<TaiKhoanKeToan> GetAllTaiKhoanKeToan()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = taikhoanketoanMgt.GetAllTaiKhoanKeToan();
            List<TaiKhoanKeToan> listTaiKhoanKeToan = (List<TaiKhoanKeToan>)resultObj.Result;
            return listTaiKhoanKeToan;
        }

        public List<ChiNhanhCongTy> GetAllChiNhanhCongTy()
        {
            ResultObj resultObj = new ResultObj();
            resultObj = chinhanhcongtyMgt.GetAllChiNhanhCongTy();
            List<ChiNhanhCongTy> listChiNhanhCongTy = (List<ChiNhanhCongTy>)resultObj.Result;
            return listChiNhanhCongTy;
        }

        public void LoadToDataGridView()
        {
            //*********632************//
            DataGridViewRow row = new DataGridViewRow();

            DataGridViewComboBoxCell cboCell = new DataGridViewComboBoxCell();
            List<TaiKhoanKeToan> lstTaiKhoanKeToan = GetAllTaiKhoanKeToan();
            cboCell.DataSource = lstTaiKhoanKeToan;
            cboCell.DisplayMember = "MaSoTK";
            cboCell.ValueMember = "Id";

            TaiKhoanKeToan tkkt = new TaiKhoanKeToan();
            for (int i = 0; i < lstTaiKhoanKeToan.Count; i++)
                if (lstTaiKhoanKeToan[i].MaSoTK == "632")
                    tkkt = lstTaiKhoanKeToan[i];
            cboCell.Value = tkkt.Id;
            row.Cells.Add(cboCell);

            cboCell = new DataGridViewComboBoxCell();
            cboCell.Items.Add("Nợ");
            cboCell.Items.Add("Có");
            cboCell.Value = "Có";
            row.Cells.Add(cboCell);

            DataGridViewCell cell = new DataGridViewTextBoxCell();
            cell.Value = tkkt.TenTK;
            row.Cells.Add(cell);

            cell = new DataGridViewTextBoxCell();
            cell.Value = 0;
            row.Cells.Add(cell);

            dgvDinhKhoan1.Rows.Add(row);

            //*********155************//
            row = new DataGridViewRow();

            cboCell = new DataGridViewComboBoxCell();
            lstTaiKhoanKeToan = GetAllTaiKhoanKeToan();
            cboCell.DataSource = lstTaiKhoanKeToan;
            cboCell.DisplayMember = "MaSoTK";
            cboCell.ValueMember = "Id";

            tkkt = new TaiKhoanKeToan();
            for (int i = 0; i < lstTaiKhoanKeToan.Count; i++)
                if (lstTaiKhoanKeToan[i].MaSoTK == "155")
                    tkkt = lstTaiKhoanKeToan[i];
            cboCell.Value = tkkt.Id;
            row.Cells.Add(cboCell);

            cboCell = new DataGridViewComboBoxCell();
            cboCell.Items.Add("Nợ");
            cboCell.Items.Add("Có");
            cboCell.Value = "Nợ";
            row.Cells.Add(cboCell);

            cell = new DataGridViewTextBoxCell();
            cell.Value = tkkt.TenTK;
            row.Cells.Add(cell);

            cell = new DataGridViewTextBoxCell();
            cell.Value = 0;
            row.Cells.Add(cell);

            dgvDinhKhoan1.Rows.Add(row);

            //*********131************//
            row = new DataGridViewRow();

            cboCell = new DataGridViewComboBoxCell();
            lstTaiKhoanKeToan = GetAllTaiKhoanKeToan();
            cboCell.DataSource = lstTaiKhoanKeToan;
            cboCell.DisplayMember = "MaSoTK";
            cboCell.ValueMember = "Id";

            tkkt = new TaiKhoanKeToan();
            for (int i = 0; i < lstTaiKhoanKeToan.Count; i++)
                if (lstTaiKhoanKeToan[i].MaSoTK == "131")
                    tkkt = lstTaiKhoanKeToan[i];
            cboCell.Value = tkkt.Id;
            row.Cells.Add(cboCell);

            cboCell = new DataGridViewComboBoxCell();
            cboCell.Items.Add("Nợ");
            cboCell.Items.Add("Có");
            cboCell.Value = "Có";
            row.Cells.Add(cboCell);

            cell = new DataGridViewTextBoxCell();
            cell.Value = tkkt.TenTK;
            row.Cells.Add(cell);

            cell = new DataGridViewTextBoxCell();
            cell.Value = 0;
            row.Cells.Add(cell);

            dgvDinhKhoan2.Rows.Add(row);

            //*********531************//
            row = new DataGridViewRow();

            cboCell = new DataGridViewComboBoxCell();
            lstTaiKhoanKeToan = GetAllTaiKhoanKeToan();
            cboCell.DataSource = lstTaiKhoanKeToan;
            cboCell.DisplayMember = "MaSoTK";
            cboCell.ValueMember = "Id";

            tkkt = new TaiKhoanKeToan();
            for (int i = 0; i < lstTaiKhoanKeToan.Count; i++)
                if (lstTaiKhoanKeToan[i].MaSoTK == "531")
                    tkkt = lstTaiKhoanKeToan[i];
            cboCell.Value = tkkt.Id;
            row.Cells.Add(cboCell);

            cboCell = new DataGridViewComboBoxCell();
            cboCell.Items.Add("Nợ");
            cboCell.Items.Add("Có");
            cboCell.Value = "Nợ";
            row.Cells.Add(cboCell);

            cell = new DataGridViewTextBoxCell();
            cell.Value = tkkt.TenTK;
            row.Cells.Add(cell);

            cell = new DataGridViewTextBoxCell();
            cell.Value = 0;
            row.Cells.Add(cell);

            dgvDinhKhoan2.Rows.Add(row);

            //*********3331************//
            row = new DataGridViewRow();

            cboCell = new DataGridViewComboBoxCell();
            lstTaiKhoanKeToan = GetAllTaiKhoanKeToan();
            cboCell.DataSource = lstTaiKhoanKeToan;
            cboCell.DisplayMember = "MaSoTK";
            cboCell.ValueMember = "Id";

            tkkt = new TaiKhoanKeToan();
            for (int i = 0; i < lstTaiKhoanKeToan.Count; i++)
                if (lstTaiKhoanKeToan[i].MaSoTK == "3331")
                    tkkt = lstTaiKhoanKeToan[i];
            cboCell.Value = tkkt.Id;
            row.Cells.Add(cboCell);

            cboCell = new DataGridViewComboBoxCell();
            cboCell.Items.Add("Nợ");
            cboCell.Items.Add("Có");
            cboCell.Value = "Nợ";
            row.Cells.Add(cboCell);

            cell = new DataGridViewTextBoxCell();
            cell.Value = tkkt.TenTK;
            row.Cells.Add(cell);

            cell = new DataGridViewTextBoxCell();
            cell.Value = this.phieuhangtra.Thue;
            row.Cells.Add(cell);

            dgvDinhKhoan2.Rows.Add(row);
        }

        private void XoaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvDinhKhoan1.CurrentRow != null)
            {
                if (dgvDinhKhoan1.CurrentRow.Index != (dgvDinhKhoan1.Rows.Count - 1))
                {
                    dgvDinhKhoan1.Rows.RemoveAt(dgvDinhKhoan1.CurrentRow.Index);
                    long sotien = Convert.ToInt64(dgvDinhKhoan1.Rows[dgvDinhKhoan1.CurrentRow.Index].Cells[3].Value.ToString());
                    long sotienTKGoc = Convert.ToInt64(dgvDinhKhoan1.Rows[0].Cells[3].Value.ToString());
                    long result = sotienTKGoc - sotien;
                    dgvDinhKhoan1.Rows[0].Cells[3].Value = result;
                }
            }
        }

        private void dgvDinhKhoan1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hti = dgvDinhKhoan1.HitTest(e.X, e.Y);
                if (hti.Type == DataGridViewHitTestType.Cell)
                {
                    dgvDinhKhoan1.ClearSelection();
                    dgvDinhKhoan1.CurrentCell = dgvDinhKhoan1.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    dgvDinhKhoan1.ContextMenuStrip = contextMenuStrip1;
                }
            }
        }

        private void XoaToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            if (dgvDinhKhoan2.CurrentRow != null)
            {
                if (dgvDinhKhoan2.CurrentRow.Index != (dgvDinhKhoan2.Rows.Count - 1))
                {
                    dgvDinhKhoan2.Rows.RemoveAt(dgvDinhKhoan2.CurrentRow.Index);
                    long sotien = Convert.ToInt64(dgvDinhKhoan2.Rows[dgvDinhKhoan2.CurrentRow.Index].Cells[3].Value.ToString());
                    long sotienTKGoc = Convert.ToInt64(dgvDinhKhoan2.Rows[0].Cells[3].Value.ToString());
                    long result = sotienTKGoc - sotien;
                    dgvDinhKhoan2.Rows[0].Cells[3].Value = result;
                }
            }
        }

        private void dgvDinhKhoan2_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                DataGridView.HitTestInfo hti = dgvDinhKhoan2.HitTest(e.X, e.Y);
                if (hti.Type == DataGridViewHitTestType.Cell)
                {
                    dgvDinhKhoan2.ClearSelection();
                    dgvDinhKhoan2.CurrentCell = dgvDinhKhoan2.Rows[e.RowIndex].Cells[e.ColumnIndex];
                    dgvDinhKhoan2.ContextMenuStrip = contextMenuStrip1;
                }
            }
        }
    }
}
